﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.View == "undefined") Axis.View = {};
if (typeof Axis.View.Member == "undefined") Axis.View.Member = {};
Axis.View.Member.ForgotPassword = {
    Init: function () {
        var thisObj = this;
        thisObj.Dialog.Init();
        thisObj.Event();
    },
    Dialog: {
        Instance: null,
        Init: function () {
            var thisObj = this;
            thisObj.Instance = new CDialog({
                title: "• Quên mật khẩu •",
                width: 600,
                zIndex: 999,
                autoOpen: false,
                draggable: false,
                open: function () {

                },
                beforeClose: function (event, ui) {
                    $(".CValidator-Error").hide();
                }
            });
            thisObj.Instance.Init("#Form-ForgotPassword");
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Instance)
                thisObj.Instance.Open();
            $("#Form-ForgotPassword").find("#imgCaptchaRefresh").trigger("click");
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Instance)
                thisObj.Instance.Close();
        }
    },
    Submit: function () {
        var thisObj = this;
        var cvalidator = new CValidator({
            zIndex: 9999
        });
        cvalidator.Init("#Form-ForgotPassword :input");
        if (!cvalidator.CheckValidity()) {
            return false;
        }
        var overlay = new COverlay({
            autoShow: false, zIndex: 9999
        });
        overlay.Init();
        var email = $.trim($("#Form-ForgotPassword #txtForgotPasswordEmail").val());
        var captcha = $("#Form-ForgotPassword").find("#txtCaptchaCode").val();
        $.Ajax({
            url: '../Accounts/Service/AccountService.asmx/ForgotPassword',
            type: 'POST',
            data: {
                pEmail: email,
                pCaptcha: captcha
            },
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    data = $.findInXml(data);
                    data = eval("(" + data + ")");
                    console.log('data', data);
                    if (data.IsSuccessfull) {
                        thisObj.Dialog.Close();
                    }
                    else {

                    }
                    var dialog = new CDialog({
                        dialogClass: "Alert",
                        width: 550,
                        title: "Thông báo",
                        beforeClose: function (event, ui) {
                            $(".CValidator-Error").hide();
                            $(".Form-ForgotPassword").find("#imgCaptchaRefresh").trigger("click");
                        },
                        buttons: [
                           {
                               text: "Đóng",
                               click: function () {
                                   $(this).dialog("close");
                               }
                           }
                        ]
                    });
                    var html = $.string.Format('<div>{0}</div>', data.Description);
                    overlay.Hide();
                    dialog.Init(html);

                }
            },
            beforeSend: function (jqXHR, settings) {
                overlay.Show();
            },
            error: function (jqXHR, textStatus, errorThrown) {
                overlay.Hide();
            },
            complete: function (XMLHttpRequest, textStatus) {

            }
        });

    },
    CheckEmail: function (pEmail) {
        var emailRe = /^([a-z0-9_\.\-\+]+)@([\da-z\.\-]+)\.([a-z\.]{2,6})$/i;
        var cvalidator = new CValidator({
            zIndex: 9999
        });
        cvalidator.Init("#txtForgotPasswordEmail");
        if (!cvalidator.CheckValidity()) {
            return false;
        }
    },
    Event: function () {
        var thisObj = this;
        $("#Form-ForgotPassword #btn_ForgotPassword_Submit").click(function () {
            thisObj.Submit();
        });
    }
}
$(function () {
    Axis.View.Member.ForgotPassword.Init();
});

function ShowDialogForgotPassword() {
    Axis.View.Member.ForgotPassword.Dialog.Open();
}